<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="tree" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形组件 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <svg-icon icon-class="tree" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="deleteDepartment(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹框 -->
      <el-dialog
        title="添加或编辑"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="50%"
      >
        <AddOrEdit
          v-if="showDialog"
          ref="dialong"
          :cur-id="curId"
          :is-edit="isEdit"
          @success="huSuccess"
          @close="close"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddOrEdit from './deptDialog.vue'
export default {
  components: {
    AddOrEdit
  },
  data() {
    return {
      // 树形数据
      list: [
      ],
      // 弹窗开关
      showDialog: false,
      // id
      curId: '',
      // 区分模态框
      isEdit: false
    }
  },
  created() {
    this.loadpartments()
  },
  methods: {
    async loadpartments() {
      try {
        // ajax数据
        const res = await getDepartments()
        // 给数据赋值并切割掉最高级的表头
        this.list = res.data.depts.shift()
        // 用写好的分层函数实现分层
        this.list = tranListToTreeData(res.data.depts)
        console.log(res, '组织架构')
      } catch (error) {
        console.log(error)
      }
    },
    // 添加
    hAdd(id) {
      this.isEdit = false
      this.showDialog = true
      this.curId = id
    },
    // 编辑
    hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      this.curId = id
    },
    // 子传父刷新页面并关闭弹框
    huSuccess() {
      this.showDialog = false
      // 重新渲染
      this.loadpartments()
    },
    // 删除ajax
    async deleteDepartment(id) {
      this.$confirm('是否删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartment(id)
        this.$message.success('删除成功')
        this.loadpartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    close() {
      this.showDialog = false
    }

  }
}
</script>
